package com.uenpay.im.ui.old.activity.alipay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Window;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;

import java.net.URLEncoder;
import java.util.Map;

public class ExternalPartner extends Activity {
    private static final int RQF_PAY = 1;
    private static final int RQF_LOGIN = 2;
    private String app_id = "", method = "", charset = "", sign_type = "",
            sign = "", timestamp = "", version = "", notify_url = "",
            biz_content = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        IMApplication.getInstance().addActivity(this);
        app_id = getIntent().getStringExtra(AliPayParameters.APP_ID);
        method = getIntent().getStringExtra(AliPayParameters.METHOD);
        charset = getIntent().getStringExtra(AliPayParameters.CHARSET);
        sign_type = getIntent().getStringExtra(AliPayParameters.SIGN_TYPE);
        sign = getIntent().getStringExtra(AliPayParameters.SIGN);
        timestamp = getIntent().getStringExtra(AliPayParameters.TIMESTAMP);
        version = getIntent().getStringExtra(AliPayParameters.VERSION);
        notify_url = getIntent().getStringExtra(AliPayParameters.NOTIFY_URL);
        biz_content = getIntent().getStringExtra(AliPayParameters.BIZ_CONTENT);
        try {
            Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(app_id, biz_content, charset, method, sign_type, timestamp, version, notify_url);
            String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
            final String orderInfo = orderParam + "&sign=" + URLEncoder.encode(sign);
            Runnable payRunnable = new Runnable() {

                @Override
                public void run() {
                    PayTask alipay = new PayTask(ExternalPartner.this);
                    Map<String, String> result = alipay.payV2(orderInfo, true);
                    Message msg = new Message();
                    msg.what = RQF_PAY;
                    msg.obj = result;
                    mHandler.sendMessage(msg);
                }
            };
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case RQF_PAY:
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultStatus = payResult.getResultStatus();

                    if (TextUtils.equals(resultStatus, "9000")) {
                        Intent intent = new Intent(BroadcastAction.PAY_ASSISTANT_ACTION);
                        sendBroadcast(intent);

                    } else {
                        if (TextUtils.equals(resultStatus, "8000")) {

                        } else {
                        }
                    }
                    break;
                case RQF_LOGIN: {
                    Toast.makeText(ExternalPartner.this, "" + msg.obj,
                            Toast.LENGTH_SHORT).show();
                }
                break;
                default:
                    break;
            }
            finish();
        }

        ;
    };

    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onDestroy() {
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }
}